﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-21-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
using System.Globalization;
namespace NMortgage.UI.Model
{
    using RAND = Troschuetz.Random;

    public sealed class BetaDistribution : ABaseValidatedType<BetaDistribution, BetaDistributionValidator>, IDistribution
    {
        #region Fields
        private const double DEFAULT_MIN = 2.0d;
        private const double DEFAULT_MAX = 6.9d;
        private const double DEFAULT_ALPHA = 1.1d;
        private const double DEFAULT_BETA = 1.1d;

        private double alpha;
        private double beta;
        private RAND.BetaDistribution distribution;
        private RAND.Generator generator;
        private double maximum;
        private double minimum;

        #endregion Fields

        #region Constructors

        public BetaDistribution()
        {
            generator = new RAND.StandardGenerator();
            distribution = new RAND.BetaDistribution(generator);

            Alpha = DEFAULT_ALPHA;
            Beta = DEFAULT_BETA;
            Minimum = DEFAULT_MIN;
            Maximum = DEFAULT_MAX;
        }

        #endregion Constructors

        #region Properties

        public double Alpha
        {
            get { return alpha; }
            set
            {
                alpha = value;
                distribution.Alpha = Alpha;
                NotifyOfPropertyChange(() => Alpha);
                NotifyOfPropertyChange(() => Beta);
            }
        }

        public double Beta
        {
            get { return beta; }
            set
            {
                beta = value;
                distribution.Beta = Beta;
                NotifyOfPropertyChange(() => Alpha);
                NotifyOfPropertyChange(() => Beta);
            }
        }

        public string Description
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, Properties.Resources.BetaRateRateDescriptionFormat, Alpha, Beta, Minimum, Maximum);
            }
        }

        public double Maximum
        {
            get { return maximum; }
            set
            {
                maximum = value;

                NotifyOfPropertyChange(() => Maximum);
                NotifyOfPropertyChange(() => Minimum);
            }
        }

        public double Minimum
        {
            get { return minimum; }
            set
            {
                minimum = value;
                NotifyOfPropertyChange(() => Maximum);
                NotifyOfPropertyChange(() => Minimum);
            }
        }

        #endregion Properties

        #region Methods

        public double GetRate()
        {
            return ScaleRandomToPercentRange(distribution.NextDouble());
        }

        private double ScaleRandomToPercentRange(double randomNummber)
        {
            return (randomNummber * (Maximum - Minimum) + Minimum) / 100d;
        }

        #endregion Methods
    }
}